package com.example.quadcopterflight;


//imported Java packages as required for this application
import java.io.IOException;

import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialProber;


//This is the definition of the Activity component class that will be used
//to handle GUI functionality for the QuadcopterFlight app.
public class QuadMainActivity extends Activity {

	private static final String TAG = "QuadMainActivity";
	UsbManager manager;
	
	//Specifies actions to be taken upon instantiation of a QuadMainActivity object.
	//onCreate overrides the onCreate method it inherits from the Activity class, then
	//sets the layout of the GUI by parsing <activity_quad_main.xml>, found in the directory
	//<QuadcopterFlight/res/layout>.
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quad_main);
	}
	
	

	
	//Not necessary to implement an options menu at this time. May be useful later. 
	/*public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.quad_main, menu);
		return true;
	}*/
	
	
	//This method handles the extraction of the text entered in the text field to be sent to the QuadIntentService class.
	public void onSendButtonClicked(View view){
		//Create an intent to be sent to the QuadIntentService class
		
		//Intent sendIntent = new Intent(this, QuadIntentService.class);
		
		//Links the text field defined in <activity_quad_main.xml> to this class
		EditText textBox = (EditText) findViewById(R.id.editText1);
		
		
		//Retrieves text entered in text box
		String enteredMessage = textBox.getText().toString();
		
		sendUART(enteredMessage);
		
		/*
		//Bundles the text retrieved into the intent for transport to QuadIntentService class.
		sendIntent.putExtra(INPUT_SERVICE, enteredMessage);
		
		//A call to the QuadIntentService, with the intent passed as a parameter. 
		startService(sendIntent);
		//Intent service should start when sendIntent is passed to it, then stop after the intent is handled.
		 */
	}
	
	
	
	
	public void sendUART(String data)
	{
		manager = (UsbManager) getSystemService(Context.USB_SERVICE);
		// Find the first available driver.
		UsbSerialDriver driver = UsbSerialProber.acquire(manager);
		if (driver != null) {
			  try {
				driver.open();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			  
			  try {
				    driver.setBaudRate(115200);
				    
				    byte buffer[] = new byte[16];
				    int numBytesRead = driver.read(buffer, 1000);
				    Log.d(TAG, "Read " + numBytesRead + " bytes.");
				   
				    
				    byte temp[] = data.getBytes();
				    /*Message msg = new Message(); 
	    			msg.obj = "\nTransmitted:\n " + data + "\n"; 
	    			Handler.sendMessage(msg);*/
				    driver.write(temp, 16);
				    
				  } catch (IOException e) {
				    // Deal with error.
				  } finally {
				    try {
						driver.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						
						e.printStackTrace();
					}
				  } 
				

			  
		}
			 

		
	}
	
	
	
}
